package cn.maidouya.org.service.impl;

import cn.maidouya.basic.exception.BusinessExcepiton;
import cn.maidouya.basic.service.impl.BaseServiceImpl;
import cn.maidouya.org.domain.Employee;
import cn.maidouya.org.domain.Shop;
import cn.maidouya.org.mapper.EmployeeMapper;
import cn.maidouya.org.mapper.ShopMapper;
import cn.maidouya.org.service.EmployeeService;
import cn.maidouya.org.service.ShopService;
import cn.maidouya.user.domain.Logininfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class ShopServiceImpl extends BaseServiceImpl<Shop> implements ShopService{
    @Autowired
    private ShopMapper shopMapper;
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private EmployeeService employeeService;

    @Override
    @Transactional
    public void save(Shop shop) {
    }

    @Override
    @Transactional
    public void update(Shop shop) {
        shopMapper.update(shop);
    }


    @Override
    @Transactional// 开启批量删除事件
    public void batchDel(Long[] ids) {
        shopMapper.batchDel(ids);
    }

    @Override
    public void settlement(Shop shop) {
        System.err.println("看看效果前： " +shop);
        // 1. 判断店铺存在 存在返回登录页面
        // 获取表单填写管理员信息
        Employee admin = shop.getAdmin();
        Employee user = employeeMapper.loadByUser(admin);
        if (user != null){
            throw new BusinessExcepiton("店铺已经注册，请直接登录，如果忘记密码，请找回密码!!");
        }
        // 2. 判断店铺不存在 则继续往后走
            //  管理员添加
        employeeService.save(admin);

            // 店铺信息添加
        shop.setAdmin(admin);
        shop.setState(1);
        shopMapper.save(shop);

            // 重修修改员工信息绑定店铺
        admin.setShop(shop);
        employeeMapper.update(admin);

        // 3.
    }
}